// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: V Fan Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.fanOffset.y
private alias object.value2 : object.hitboxT
private alias object.value3 : object.hitboxB

// States
private alias 0 : VFAN_DEACTIVATE
private alias 1 : VFAN_IDLE
private alias 2 : VFAN_ACTIVE

// Player Aliases
private alias object.state : player.state
private alias object.ypos : player.ypos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animationSpeed : player.animationSpeed
private alias object.animation : player.animation
private alias object.scrollTracking : player.scrollTracking
private alias object.tileCollisions : player.tileCollisions

private alias object.value1 : player.timer


// ========================
// Function Declarations
// ========================

reserve function VFan_DebugDraw
reserve function VFan_DebugSpawn


// ========================
// Function Definitions
// ========================

private function VFan_DebugDraw
	DrawSprite(0)
end function


private function VFan_DebugSpawn
	CreateTempObject(TypeName[V Fan], 0, object.xpos, object.ypos)
	object[tempObjectPos].speed = 48
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case VFAN_DEACTIVATE
		if object.speed > 0
			object.speed--
		else
			object.state++
		end if
		object.frame = object.animationTimer
		object.frame /= 48
		object.animationTimer += object.speed
		object.animationTimer %= 288
		break

	case VFAN_IDLE
		object.timer++
		if object.timer == 30
			object.timer = 0
			object.speed = 48
			object.state++
		end if
		break

	case VFAN_ACTIVE
		object.frame = object.animationTimer
		object.frame /= 48
		object.animationTimer += object.speed
		object.animationTimer %= 288

		object.timer++
		if object.timer == 180
			object.timer = 0
			object.state = VFAN_DEACTIVATE
		end if
		
		// (This little thing done with temp0, the result isn't actually used anywhere else in this script)
		// Perhaps this object used to use this variable for oscillation rather than the oscillation table itself?
		temp0 = oscillation
		temp0 <<= 2
		
		GetTableValue(object.hitboxT, 10, StageSetup_oscillationTable)
		object.hitboxT >>= 8
		object.hitboxB = object.hitboxT
		object.hitboxT -= 112
		object.hitboxB += 32
		object.fanOffset.y = object.hitboxT
		object.fanOffset.y <<= 16
		object.fanOffset.y += object.ypos
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -64, object.hitboxT, 64, object.hitboxB, currentPlayer, 0, 0, 0, 0)
			if checkResult == true
				player[currentPlayer].animation 		= ANI_TWIRL
				player[currentPlayer].animationSpeed 	= 0x50
				player[currentPlayer].scrollTracking 	= true
				player[currentPlayer].gravity 			= 1
				player[currentPlayer].state 			= Player_State_Air
				player[currentPlayer].tileCollisions 	= true
				player[currentPlayer].speed 			= player[currentPlayer].xvel
				player[currentPlayer].timer 			= 0
				player[currentPlayer].yvel 				= 0
				temp1 = object.fanOffset.y
				temp1 -= player[currentPlayer].ypos
				temp1 >>= 4
				player[currentPlayer].ypos += temp1
			end if
		next
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-16, -12, 32, 24, 206, 181)
		SpriteFrame(-16, -12, 32, 24, 239, 181)
		SpriteFrame(-16, -12, 32, 24, 206, 206)
		SpriteFrame(-16, -12, 32, 24, 239, 206)
		SpriteFrame(-16, -12, 32, 24, 206, 231)
		SpriteFrame(-16, -12, 32, 24, 239, 231)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -12, 32, 24, 376, 820)
		SpriteFrame(-16, -12, 32, 24, 409, 820)
		SpriteFrame(-16, -12, 32, 24, 376, 845)
		SpriteFrame(-16, -12, 32, 24, 409, 845)
		SpriteFrame(-16, -12, 32, 24, 376, 870)
		SpriteFrame(-16, -12, 32, 24, 409, 870)
	end if

	foreach (TypeName[V Fan], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].speed = 36
	next

	SetTableValue(TypeName[V Fan], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(VFan_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(VFan_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-16, -12, 32, 24, 206, 181)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -12, 32, 24, 376, 820)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
